package terrain;

import java.util.logging.Logger;

import test.Constants;

import com.jme3.app.Application;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;

public class Wall {
	private static final Logger logger = Logger.getLogger(Application.class
			.getName());
	
	private static final String simpleTex = "Common/MatDefs/Misc/SimpleTextured.j3md";
	private static final String earthTex = "texture/earth-side.png";
	private static final String stoneTex = "texture/stone-side.png";
	private static final String stonewallTex = "texture/stonewall-side.png";
	
	private Geometry earthSideNorth;
	private Geometry earthSideSouth;
	private Geometry earthSideWest;
	private Geometry earthSideEast;

	private Geometry stoneSideNorth;
	private Geometry stoneSideSouth;
	private Geometry stoneSideWest;
	private Geometry stoneSideEast;

	private Geometry stonewallSideNorth;
	private Geometry stonewallSideSouth;
	private Geometry stonewallSideWest;
	private Geometry stonewallSideEast;
	
	public Wall(AssetManager assetManager) {
		Box b = new Box(Vector3f.ZERO, 0.5f, 1f, 0);
		
		//Generic Walls
		Geometry genericNorth = new Geometry("Side North", b);
		genericNorth.rotate(new Quaternion(1, 0, 0, 1));
		genericNorth.move(0.5f, 1, 1);
		
		Geometry genericSouth = genericNorth.clone(); 
		genericSouth.rotate(new Quaternion(0, 1, 0, 1));
		genericSouth.rotate(new Quaternion(0, 1, 0, 1));
		genericSouth.move(0, -1, 0);
		
		Geometry genericWest = genericNorth.clone();
		genericWest.rotate(new Quaternion(0, 1, 0, 1));
		genericWest.move(-0.5f, -0.5f, 0);		
		
		Geometry genericEast = genericNorth.clone();
		genericEast.rotate(new Quaternion(0, -1, 0, 1));
		genericEast.move(0.5f, -0.5f, 0);
		
		//Earth Walls
		Material matEarth = new Material(assetManager, simpleTex);
		Texture texEarth = assetManager.loadTexture(earthTex);
		matEarth.setTexture("m_ColorMap", texEarth);
				
		earthSideNorth = genericNorth.clone();
		earthSideNorth.setName("Earth Side North");
		earthSideNorth.setMaterial(matEarth);
		
		earthSideSouth = genericSouth.clone();
		earthSideSouth.setName("Earth Side South");
		earthSideSouth.setMaterial(matEarth);
		
		earthSideWest = genericWest.clone();
		earthSideWest.setName("Earth Side West");
		earthSideWest.setMaterial(matEarth);
		
		earthSideEast = genericEast.clone();
		earthSideEast.setName("Earth Side East");
		earthSideEast.setMaterial(matEarth);
		
		//Stone Rock
		Material matStone = new Material(assetManager, simpleTex);
		Texture texStone = assetManager.loadTexture(stoneTex);
		matStone.setTexture("m_ColorMap", texStone);
		
		stoneSideNorth = genericNorth.clone();
		stoneSideNorth.setName("Stone Side North");
		stoneSideNorth.setMaterial(matStone);
		
		stoneSideSouth = genericSouth.clone();
		stoneSideSouth.setName("Stone Side South");
		stoneSideSouth.setMaterial(matStone);
		
		stoneSideWest = genericWest.clone();
		stoneSideWest.setName("Stone Side West");
		stoneSideWest.setMaterial(matStone);
		
		stoneSideEast = genericEast.clone();
		stoneSideEast.setName("Stone Side East");
		stoneSideEast.setMaterial(matStone);
		
		//Stone Walls
		Material matWall = new Material(assetManager, simpleTex);
		Texture texWall = assetManager.loadTexture(stonewallTex);
		matWall.setTexture("m_ColorMap", texWall);
		
		stonewallSideNorth = genericNorth.clone();
		stonewallSideNorth.setName("Stonewall Side North");
		stonewallSideNorth.setMaterial(matWall);
		
		stonewallSideSouth = genericSouth.clone();
		stonewallSideSouth.setName("Stonewall Side South");
		stonewallSideSouth.setMaterial(matWall);
		
		stonewallSideWest = genericWest.clone();
		stonewallSideWest.setName("Stonewall Side West");
		stonewallSideWest.setMaterial(matWall);
		
		stonewallSideEast = genericEast.clone();
		stonewallSideEast.setName("Stonewall Side East");
		stonewallSideEast.setMaterial(matWall);
	}
	
	public Geometry getSide(int direction, int type) {
		if (type == Constants.STONE) {
			if (direction == Constants.NORTH) {
				return stoneSideNorth.clone();
			} else if (direction == Constants.SOUTH) {
				return stoneSideSouth.clone();
			} else if (direction == Constants.EAST) {
				return stoneSideEast.clone();
			} else if (direction == Constants.WEST) {
				return stoneSideWest.clone();
			} else {
				logger.warning("wrong stone direction transmitted: " + direction);
				return null;
			}
		} else if (type == Constants.EARTH) {
			if (direction == Constants.NORTH) {
				return earthSideNorth.clone();
			} else if (direction == Constants.SOUTH) {
				return earthSideSouth.clone();
			} else if (direction == Constants.EAST) {
				return earthSideEast.clone();
			} else if (direction == Constants.WEST) {
				return earthSideWest.clone();
			} else {
				logger.warning("wrong earth direction transmitted: " + direction);
				return null;
			}
		} else if (type == Constants.STONEWALL) {
			if (direction == Constants.NORTH) {
				return stonewallSideNorth.clone();
			} else if (direction == Constants.SOUTH) {
				return stonewallSideSouth.clone();
			} else if (direction == Constants.EAST) {
				return stonewallSideEast.clone();
			} else if (direction == Constants.WEST) {
				return stonewallSideWest.clone();
			} else {
				logger.warning("wrong earth direction transmitted: " + direction);
				return null;
			}
		} else {
			logger.warning("wrong wall type transmitted: " + type);
			return null;
		}
	}
}
